Write a program to check if a given number is a perfect square or not.
Write a program to check if a given number is a perfect square or not.
735
19-Apr-2023
Aryan Kumar
22-Apr-2023In this program, we define a method isPerfectSquare that takes an integer num as input and returns a boolean value indicating whether or not num is a perfect square.
To check if num is a perfect square, we calculate the square root of num using the Math.sqrt method, cast it to an integer, and assign it to the variable sqrt. If sqrt * sqrt is equal to num, then num is a perfect square and we return true. Otherwise, we return false.
In the main method, we test the isPerfectSquare method with the number 16 and print out a message indicating whether or not 16 is a perfect square. You can replace 16 with any other number to test the program with a different input.
Krishnapriya Rajeev
20-Apr-2023The following Java program checks whether the given number is a perfect square or not: